package com.liangfeng.breezebrowser.browsermvp.view.webview;

import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

import com.apkfuns.logutils.LogUtils;
import com.liangfeng.breezebrowser.bean.BrowHistoryBean;
import com.liulishuo.magicprogresswidget.MagicProgressBar;

import io.realm.Realm;

/**
 * Created by mozhifeng on 2017/5/26.
 */

public class BrowserChromeClient extends WebChromeClient {

    private MagicProgressBar mpb;
    private Realm realm;

    public BrowserChromeClient(MagicProgressBar mpb, Realm realm) {
        this.mpb = mpb;
        this.realm = realm;
    }

    //加载进度
    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if (newProgress == 100) {//加载完成
            mpb.setVisibility(View.GONE);
        } else {//正在加载
            mpb.setVisibility(View.VISIBLE);
            mpb.setSmoothPercent(newProgress);
        }
        super.onProgressChanged(view, newProgress);
    }

    //获取页面标题
    @Override
    public void onReceivedTitle(WebView view, String title) {
        LogUtils.e(title+">>>>>"+view.getUrl());
        realm.beginTransaction();//开启事务
        BrowHistoryBean history = realm.createObject(BrowHistoryBean.class);
        history.setTitle(title);
        history.setUrl(view.getUrl());
        history.setMark(false);
        realm.commitTransaction();//提交事务
    }
}
